<template lang="pug">
  .CreateAccountModal
    Modal(
      v-model="isShowAdjusSuperiorModal"
      title="调整上级组织信息"
      class-name="vertical-center-modal"
      width="400"
    )
      Form.ivu-row.has-rules-item(ref="formData" :model="formData" :rules="rules" :label-width="135")
        FormItem.form-item.ivu-col(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
          template(v-if="item.type === 'input'")
            Input(v-model="formData[item.model]" :placeholder="item.placeholder" :disabled="item.isDisabled")
          template(v-if="item.type === 'select'")
            Select(v-model="formData[item.model]" :placeholder="item.placeholder" filterable)
              Option(v-for="(c_item, c_index) of arrListDown[item.options]" :key="c_index" :value="c_item.id") {{c_item.title}}
          template(v-if="item.type === 'datePicker'")
            DatePicker.w-100(:value="formData[item.model]" :placeholder="item.placeholder" type="date")
          template(v-if="item.type === 'cascader'")
            Input(v-model="formData[item.model]" v-show="false")
            Cascader(v-model="formData[item.arrModel]" :data="parentSource" clearable @on-change="handleTreeChange" :change-on-select="true")
              Input(type="text" icon="ios-arrow-down" :value="selectParentObj.label")
          template(v-if="item.type === 'switch'")
            i-switch(v-model="formData[item.model]")
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSubmit") 保存
</template>

<script>
import { Common } from '@/utils/common.js'
// import { UserApi } from '@/api/User.api.js'
import UserApi from '@/api/User.api.js'
import { OrganizationApi } from '@/api/Organization.api.js'
export default {
  name: 'AdjusSuperiorModal',

  watch: {
  },

  computed: {
  },

  data () {
    return {
      isShowAdjusSuperiorModal: false,
      createOrganizationModalTitle: '',
      basicDataId: null,
      formData: {
        id: '',
        parentName: '',
        parentId: '',
        parentIdArr: [],
        effectTime: new Date()
      },
      formItem: [
        { type: 'input', label: '当前上级组织：', placeholder: '请输入当前上级组织', prop: 'parentName', model: 'parentName', style: 'ivu-col-span-24', isDisabled: true },
        { type: 'cascader', label: '调整后上级组织：', placeholder: '请选择调整后上级组织', prop: 'parentId', model: 'parentId', arrModel: 'parentIdArr', style: 'ivu-col-span-24', options: 'parentSource' },
        { type: 'datePicker', label: '生效日期：', placeholder: '请选择生效日期', prop: 'effectTime', model: 'effectTime', style: 'ivu-col-span-24' }
      ],
      rules: {
      },
      arrListDown: {},
      parentSource: [],
      areaTree: [], // 中国所有城市树
      selectParentObj: {}
    }
  },

  mounted () {
    this.areaTree = JSON.parse(localStorage.getItem('areaTree')) // 获取中国所有城市树
  },

  methods: {
    // 监听所有调用的方法
    // listenCall(methodsWords, param) {
    //   this[methodsWords](param)
    // },

    cancel () {
      this.isShowAdjusSuperiorModal = false
    },

    show () {
      this.isShowAdjusSuperiorModal = true
      this.$refs.formData.resetFields()
      console.log('点击是的show()')
    },

    // 根据ID获取信息
    async getModel (id, currentParentName) {
      let res = await OrganizationApi.getUpdateParentModel({ id: id })
      console.log('获取modal res: ', res)
      if (res.status === 200 && res.data.success) {
        var data = res.data.data
        this.parentSource = data.parentSource
        this.formData.id = id
        this.formData.parentName = currentParentName
      } else {
        // this.treeData = []
      }
    },

    handleTreeChange (value, selectedData) {
      console.log('value: ', value)
      console.log('selectedData: ', selectedData)
      this.selectParentObj = selectedData[selectedData.length - 1]
      this.formData.parentIdArr = value
      this.formData.parentId = selectedData[selectedData.length - 1].value
    },

    // 提交表单
    async handleSubmit () {
      console.log('提交表单：', this.formData)
      let res = await OrganizationApi.updateParent(this.formData)
      console.log('获取modal res: ', res)
      if (res.status === 200 && res.data.success) {
        this.isShowAdjusSuperiorModal = false
        this.$Message.success(res.data.message)
        this.$emit('handleSubmit')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
